package com.lsh.util.result;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据返回封装类
 * @author Administrator
 * @date 2019/10/28 16:18
 */
public class Result {

    private Object code; //状态码
    private String message; //状态信息

    @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
    private Map<String, Object> data = new HashMap<String, Object>(); //返回数据

    public Result(){

    }

    public Result(Object code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(Object code, String message, Map<String, Object> data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Object getCode() {
        return code;
    }

    public void setCode(Object code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public Result addExtraData(String key, Object value) {
        data.put(key, value);
        return this;
    }

    public static Result success() {
        return new Result(
                BaseResult.SUCCESS.getCode(),
                BaseResult.SUCCESS.getMessage()
        );
    }

    public static Result fail() {
        return new Result(
                BaseResult.FAIL.getCode(),
                BaseResult.FAIL.getMessage()
        );
    }

    public static Result tokenExpired() {
        return new Result(
                BaseResult.TOKEN_EXPIRED.getCode(),
                BaseResult.TOKEN_EXPIRED.getMessage()
        );
    }

    public static Result unauthorized() {
        return new Result(
                BaseResult.UNAUTHORIZED.getCode(),
                BaseResult.UNAUTHORIZED.getMessage()
        );
    }

}
